﻿using Ics.NuttxSharp.Bsp.CanFd;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace Ics.NuttxSharp.Test
{
    internal class Program
    {
        [DllImport("*")]
        internal static extern void SetIcsNativeaotActiveCode(byte[] activeCode);
        static void Main(string[] args)
        {
            var activeCode = new byte[] { 0x18, 0x70, 0x51, 0x1A, 0xAA, 0x6C, 0xB5, 0x08, 0xBB, 0xC1, 0xD7, 0xAE, 0x70, 0xFE, 0xE4, 0x8F, 0x27, 0xD1, 0xED, 0x99, 0xD8, 0x8C, 0x70, 0xA8, 0x6C, 0xA3, 0x3D, 0xFA, 0x93, 0xCF, 0xB9, 0x66, };
            var platform = RuntimeInformation.ProcessArchitecture;
            if (platform == Architecture.Arm)
            {
                SetIcsNativeaotActiveCode(activeCode);
            }

            //到了这一步已经初始化了CAN了
            //编写一个简单的回显程序
            var canDevice = new CanFdDevice(0);
            canDevice.EventReceived+= (s, e) =>
            {
                var ev = e.Event;
                switch (ev.EventType)
                {
                    case CanFdEventType.CanFdEventType_RxMessage:
                        {
                            var rxMsg = ev.RxMessage!;
                            Debug.WriteLine($"[CAN{rxMsg.CanIndex}] RX ID=0x{rxMsg.Identifier:X} DLC={rxMsg.Dlc} DATA={BitConverter.ToString(rxMsg.Data, 0, (int)CanFdDevice.GetDataLengthFromDlc(rxMsg.Dlc))}");
                            //回显
                            var txMsg = new CanFdTxMessage
                            {
                                Identifier = rxMsg.Identifier,
                                IdType = rxMsg.IdType,
                                TxFrameType = rxMsg.RxFrameType,
                                Dlc = rxMsg.Dlc,
                                ErrorStateIndicator = rxMsg.ErrorStateIndicator,
                                BitRateSwitch = rxMsg.BitRateSwitch,
                                FDFormat = rxMsg.FDFormat,
                                TxEventFifoControl = 0,
                                MessageMarker = 0,
                                Data = rxMsg.Data,
                            };
                            canDevice.Send(txMsg);
                            
                        }
                        break;
                    case CanFdEventType.CanFdEventType_Error:
                        {
                            var (canIndex, errorCode) = ev.ErrorMessage!.Value;
                            Debug.WriteLine($"[CAN{canIndex}] ERROR CODE=0x{errorCode:X}");
                        }
                        break;
                    case CanFdEventType.CanFdEventType_TxComplete:
                        {
                            var canIndex = ev.TxCompleteCanIndex!;
                            Debug.WriteLine($"[CAN{canIndex}] TX COMPLETE");
                        }
                        break;
                }
            };

            Console.WriteLine("start echo can server.");

            Thread.Sleep(Timeout.Infinite);
        }

    }

}
